package com.example.login.impl;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.Toast;

import com.example.common.entity.ResponseEntity;
import com.example.login.LoginContract;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

@SuppressWarnings("ResultOfMethodCallIgnored")
@SuppressLint("CheckResult")
public class LoginPresenterImpl implements LoginContract.LoginPresenter {
    private LoginContract.LoginView mView;
    private static final String KEY_CODE = "123456";

    public LoginPresenterImpl(LoginContract.LoginView view) {
        mView = view;
    }

    @Override
    public void fetchVerifyCode() {
        Observable.intervalRange(System.currentTimeMillis(), 1, 0, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> countdown())
                .subscribe(aLong -> {
                    Toast.makeText(((Context) mView), "验证码:" + KEY_CODE, Toast.LENGTH_LONG).show();
                });
    }

    /**
     * 倒计时
     */
    private void countdown() {
        int count = 10;
        Observable.intervalRange(1, count, 0, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(value -> {
                    mView.onCountDown(count - value);
                });
    }

    @Override
    public void login(CharSequence telNum, CharSequence verifyCode) {
        Observable.<ResponseEntity>create(e -> {
            Thread.sleep(3000);
            ResponseEntity entity = new ResponseEntity();
            if (KEY_CODE.contentEquals(verifyCode)) {
                entity.code = ResponseEntity.OK;
                entity.message = "";
            } else {
                entity.code = ResponseEntity.ERROR;
                entity.message = "验证码错误";
            }
            e.onNext(entity);
            e.onComplete();
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.onLogging();
                    }

                    @Override
                    public void onNext(ResponseEntity entity) {
                        if(entity.isSuccess()){
                            mView.onLoginSuccess();
                        }else{
                            mView.onLoginError(entity.message);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.onLoginError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
